package org.framework.helper;

import org.framework.ConfigHelper;
import org.framework.annotation.Controller;
import org.framework.annotation.Service;
import org.framework.utils.ClassUtil;

import java.util.HashSet;
import java.util.Set;

/**
 * 类操作助手
 *
 * 获取特定注解的类
 * @Author xuelongjiang
 */
public class ClassHelper {


    private static final Set<Class<?>>  CLASS_SET;

    static {
        String basePackage = ConfigHelper.getAppBasePackage();
        CLASS_SET = ClassUtil.getClassSet(basePackage);
    }


    /**
     * 获取应用包下的所有类
     * @return
     */
    public static Set<Class<?>> getClassSet(){
        return CLASS_SET;
    }


    /**
     * 获取应用包下的 所有controller
     */
    public static Set<Class<?>> getControllerClassSet(){

        Set<Class<?>> classSet = new HashSet<Class<?>>();
        for(Class<?> cls : CLASS_SET){
            if(cls.isAnnotationPresent(Controller.class)){
                classSet.add(cls);
            }
        }
        return classSet;
    }



    /**
     * 获取应用包下的所有server 类
     */
    public static  Set<Class<?>> getServerClassSet(){

        Set<Class<?>> classSet = new HashSet<Class<?>>();
        for(Class<?> cls: CLASS_SET){
            if(cls.isAnnotationPresent(Service.class)){
                classSet.add(cls);
            }
        }
        return classSet;
    }


    /**
     * 获取应用包下的所有bean(包括 contrller,server)
     */
    public static Set<Class<?>> getBeanClassSet(){

        Set<Class<?>> beanClassSet = new HashSet<Class<?>>();
        beanClassSet.addAll(getControllerClassSet());
        beanClassSet.addAll(getServerClassSet());
        return  beanClassSet;
    }



}
